package com.lsk.admin.member;

import com.google.gson.annotations.Until;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lsk.util.PageUtil;
import com.lsk.util.ReturnData;
import com.lsk.util.UtilUUID;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MemberService {

    /**
     * 会员数据
     */
    public Map memberListDataService(int page,int limit,String key1){
        String sql = "SELECT *from yx_member ";
        if (key1.equals("")==false) {
            sql += " where name like '%" + key1 + "%' ORDER BY createTime desc  ";
        }else {
            sql+="ORDER BY createTime desc ";
        }
        PageUtil pageUtil=new PageUtil(page,limit,sql);
        List<Record> list=pageUtil.getList();
        int count=pageUtil.getTotalRows();
        for (int i=0;i<list.size();i++){
            Record record=list.get(i);
            String grade=record.getStr("grade");
            if ("0".equals(grade)){
                record.set("grade","普通会员");
            }else {
                record.set("grade","VIP会员");
            }
            list.set(i,record);
        }
        Map map=new HashMap();
        map.put("code",0);
        if (count>0){
            map.put("count",count);
            map.put("data",list);
        }else {
            map.put("count",0);
            map.put("msg","暂无数据");
        }
        return map;

    }
    /**
     * 下级会员数据
     */
    public Map sonMemberDataService(int page,int limit,String parentId){

        String sql="SELECT *from yx_member  where parentId='"+parentId+"' order by createTime desc ";

        PageUtil pageUtil=new PageUtil(page,limit,sql);
        List<Record> list=pageUtil.getList();
        int count=pageUtil.getTotalRows();
        for (int i=0;i<list.size();i++){
            Record record=list.get(i);
            String grade=record.getStr("grade");
            if ("0".equals(grade)){
                record.set("grade","普通会员");
            }else {
                record.set("grade","VIP会员");
            }
            list.set(i,record);
        }
        Map map=new HashMap();
        map.put("code",0);
        if (count>0){
            map.put("count",count);
            map.put("data",list);
        }else {
            map.put("count",0);
            map.put("msg","暂无数据");
        }
        return map;

    }


    /**
     * 获利记录
     */

    /**
     * 下级会员数据
     */
    public Map memberRebateLogDataService(int page,int limit,String memberId){

        String sql="SELECT *from shop_rebate_log where gainerId='"+memberId+"'  ORDER BY createTime desc  ";

        PageUtil pageUtil=new PageUtil(page,limit,sql);
        List<Record> list=pageUtil.getList();
        int count=pageUtil.getTotalRows();
        for (int i=0;i<list.size();i++){
            Record record=list.get(i);
            String type=record.getStr("type");
            if ("0".equals(type)){
                record.set("type","消费返利");
            }else {
                record.set("type","升级会员返利");
            }

            String grade=record.getStr("grade");
            if ("0".equals(grade)){
                record.set("grade","一级返利");
            }else {
                record.set("grade","二级返利");
            }

            list.set(i,record);
        }
        Map map=new HashMap();
        map.put("code",0);
        if (count>0){
            map.put("count",count);
            map.put("data",list);
        }else {
            map.put("count",0);
            map.put("msg","暂无数据");
        }
        return map;

    }

    public Map memberAccountLogDataService(int page,int limit,String memberId){

        String sql="SELECT *from shop_member_account_info where memberId='"+memberId+"' ORDER BY createTime desc  ";

        PageUtil pageUtil=new PageUtil(page,limit,sql);
        List<Record> list=pageUtil.getList();
        int count=pageUtil.getTotalRows();
        for (int i=0;i<list.size();i++){
            Record record=list.get(i);
            String type=record.getStr("type");
            if ("0".equals(type)){
                record.set("type","－");
            }else {
                record.set("type","＋");
            }
            list.set(i,record);
        }
        Map map=new HashMap();
        map.put("code",0);
        if (count>0){
            map.put("count",count);
            map.put("data",list);
        }else {
            map.put("count",0);
            map.put("msg","暂无数据");
        }
        return map;

    }
    /**
     * 成为分销商
     * and shopId='"+shopId+"'
     */
    public PageUtil MemberService(String page,String limit,String shopId){

        String sql="SELECT * from yx_member where apply='1'and shopId='"+shopId+"'";

        String countSql="SELECT count(*) from yx_member where apply='1'and shopId='"+shopId+"'";

        PageUtil pageUtil=new PageUtil(Integer.parseInt(page),Integer.parseInt(limit),sql,countSql);

        return pageUtil;
    }
    /**
     * 通过成为分销商
     */
    public ReturnData passMemberServie(String id){

        ReturnData returnData=new ReturnData();

        String sql="UPDATE yx_member SET apply='2' where id='"+id +"'";

        int i= Db.update(sql);

        //往  分销商审核记录表  添加记录
        this.distributorAudit(id,"2","分销商审核通过");



        if(i>0){
            returnData.setCode("000000");
            returnData.setMsg("成功");

        }else {

            returnData.setCode("111111");
            returnData.setMsg("失败");
        }

        return returnData;
    }

    /**
     * //往  分销商审核记录表  添加记录
     * @param memberId
     * @param state
     * @return
     */
    public ReturnData distributorAudit(String memberId,String state,String remake){
        ReturnData returnData=new ReturnData();
        try {

            String membersql = "SELECT * FROM yx_member WHERE id='" + memberId + "'";
            Record member = Db.findFirst(membersql);
            Record yx_distributor_audit = new Record();
            yx_distributor_audit.set("id", UtilUUID.getUUID()).set("memberId", memberId).set("memberName", member.get("name")).set("tel", member.get("tel"))
                    .set("state", state).set("remake", remake);
            Db.save("yx_distributor_audit",yx_distributor_audit);
            returnData.setResult(true);
        }catch (Exception e) {
            e.printStackTrace();
            returnData.setResult(false);
        }
        return returnData;
    }

    /**
     * 驳回分销商
     */
    public ReturnData turnMemberService(String id){

        ReturnData returnData=new ReturnData();

        String sql="UPDATE yx_member SET apply='3' where id='"+id +"'";

        //添加分销商审核记录
        this.distributorAudit(id,"3","申请已拒绝");

        int i= Db.update(sql);

        if(i>0){

            returnData.setCode("000000");
            returnData.setMsg("成功");

        }else {

            returnData.setCode("111111");
            returnData.setMsg("失败");

        }

        return returnData;
    }

}
